package com.scholarship.modules.admin.utils;

import com.scholarship.modules.admin.entity.UserInfo;

import java.util.*;

/**
 * 计算排名工具类
 *
 * @Author Z.J.H
 * @create 2021/5/11 12:05
 */
public class RankingUtil {

    //是否升序
    private static final Boolean ISASC = true;

    /**
     * 通过加权平均分排序,并进行排名(默认:升序)
     * @param userInfos
     * @return
     */
    public static List<UserInfo> rankingScoreInUserInfo(List<UserInfo> userInfos) {
        return rankingScoreInUserInfo(userInfos,ISASC);
    }

    /**
     * 通过加权平均分排序,并进行排名
     * @param userInfos
     * @param isAsc 是否升序
     * @return
     */
    public static List<UserInfo> rankingScoreInUserInfo(List<UserInfo> userInfos, Boolean isAsc) {
        List<UserInfo> t1 = userInfos;
        Collections.sort(t1, new Comparator<UserInfo>() {
            @Override
            public int compare(UserInfo t1, UserInfo t2) {
                int temp = 0;
                if(t1.getScore() > t2.getScore()) {
                    temp = 1;
                }else if(t1.getScore() < t2.getScore()) {
                    temp = -1;
                }
                return isAsc ? temp : -temp;
            }
        });

        Integer rank = 1, len = t1.size();   //初始排名
        Double before = t1.get(0).getScore(); //前一位加权平均分
        for(int i = 0;i < len;i++) {
//            if(!t1.get(i).getScore().equals(before)) {
//                before = t1.get(i).getScore();
//                rank++;
//            }
//            t1.get(i).setRank(rank);
            t1.get(i).setRank(rank++);
        }

        return t1;
    }
}
